var url = '';
var jsonData = '';

$(function() {
	loadUser();
});

function subimtUpdateUserBtn() { 
	var form = $("form[name=updateUserform]");  
    fileUrl = ctx+'/UserInfoController/updateUserInfo';
 
    var options  = {
        url:fileUrl,    
        type:'post',    
        success:function(data)    
        {   
            var jsondata = eval("("+data+")");
        	if(jsondata.status==200){
        		 
        	}
        }    
    };   
    
    form.ajaxSubmit(options); 
    return false;
} 


function loadUser() {
	url = ctx + '/UserInfoController/getUserInfo';
	jsonData = {};
	var  returnData = commonAjax();
	
	var initHtml = initManageUserHtml(returnData);
	
	$("#manageUserTBody").html(initHtml);
}

function initManageUserHtml(data) {
	var returnHtml = "";
	if (data.length == 0)
		returnHtml = '<tr><td colspan="5">暂无数据</td><td></td></tr>';
	else
		$.each(data, function(index, item) {
			 
		 returnHtml +='<tr class="gradeA">'
         +'<td id="'+item.id+'">'+item.userName+'</td>'
         +'<td>'+item.password+'</td>'
         +'<td>'+item.sex+'</td>'
         +'<td>'+item.mobilePhone+'</td>'
         +'<td>'+item.email+'</td>'
         +'<td>'+item.address+'</td>'
         +'<td><a href="#" onclick="initModel('+item.id+');" data-toggle="modal" data-target="#updateUser">修改</a></td>'
         +'<td><a onclick="deleteUser('+item.id+');">删除</a></td>'
         +'</tr>';
		});
	
		return returnHtml;
}

function deleteUser(id) {
	url = ctx + '/UserInfoController/deleteUserInfo';
	jsonData = {userId:id};
	var  returnData = commonAjax();
}

function initModel(id) {
	url = ctx + '/UserInfoController/getSingleUserInfo';
	jsonData = {userId:id};
	var  returnData = commonAjax();
	 
	$("#userId").val(id);
	$("#userName").val(returnData[0].userName);
	$("#password").val(returnData[0].password);
	$("#address").val(returnData[0].address);
	$("#email").val(returnData[0].email);
	$("#mobilePhone").val(returnData[0].mobilePhone);
	$("#sex").val(returnData[0].sex);
	
}
 

function commonAjax(){
	var result = '';
 	$.ajax({
		type : "GET",
		url: url,
	 	data : jsonData,
	 	async: false,
		dataType : "json",
		success: function(data) {
				result = data;
			}
		});
 	//console.info("result = "+result);
 	return result;
}